package com.gee.spring.cloud.data.transform.core.center.config.handler;

import cn.hutool.core.util.HexUtil;
import com.gee.spring.cloud.common.core.api.result.ApiResult;
import com.gee.spring.cloud.common.core.entity.center.config.Device;
import com.gee.spring.cloud.common.starter.config.event.CenterConfigEventHandler;
import com.gee.spring.cloud.common.starter.feign.center.config.DeviceClient;
import com.gee.spring.cloud.data.transform.core.dispatch.BaseDataDispatcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gepengjun
 * @since 2024/3/19 14:31
 */
@Component
@Slf4j
public class DeviceHandler extends CenterConfigEventHandler<Device> {

    private final ConcurrentHashMap<Long, Device> DEVICE_MAP = new ConcurrentHashMap<>();

    @Autowired
    private DeviceClient deviceClient;

    @Autowired
    private BaseDataDispatcher baseDataDispatcher;

    public Device getById(Long id){
        return DEVICE_MAP.computeIfAbsent(id, devId -> deviceClient.getById(devId).getData());
    }

    public Device getByDevIdentity(long devIdentity){
        Optional<Device> deviceOptional = DEVICE_MAP.values().stream()
                .filter(device -> devIdentity == device.getDevIdentity())
                .findFirst();
        if (deviceOptional.isPresent()){
            return deviceOptional.get();
        }else {
            ApiResult<Device> deviceApiResult = deviceClient.getByDevIdentity(devIdentity);
            Device device = deviceApiResult.getData();
            if (deviceApiResult.isSuccess() && device != null){
                DEVICE_MAP.put(device.getId(), device);
                return device;
            }else {
                log.error("根据设备标识未获取到设备, 设备标识:{}", HexUtil.toHex(devIdentity).toUpperCase());
                return null;
            }
        }
    }

    @Override
    public void handlerUpdate(List<Device> dataList, List<Device> oldList) {
        dataList.forEach(device ->{
            DEVICE_MAP.computeIfPresent(device.getId(), (id, old) -> device);
            baseDataDispatcher.updateDevice(device);
        });
    }

    @Override
    public void handlerDelete(List<Device> oldList) {
        oldList.forEach(device -> {
            DEVICE_MAP.remove(device.getId());
            baseDataDispatcher.removeDevice(device);
        });
    }

    public Device getByDevCode(String devCode) {
        Optional<Device> deviceOptional = DEVICE_MAP.values().stream()
                .filter(device -> devCode.equals(device.getDevCode()))
                .findFirst();
        if (deviceOptional.isPresent()){
            return deviceOptional.get();
        }else {
            ApiResult<Device> deviceApiResult = deviceClient.getByDevCode(devCode);
            Device device = deviceApiResult.getData();
            if (deviceApiResult.isSuccess() && device != null){
                DEVICE_MAP.put(device.getId(), device);
                return device;
            }else {
                log.error("根据设备代号未获取到设备, 设备代号:{}", devCode);
                return null;
            }
        }
    }

    public Device getByDevName(String devName) {
        Optional<Device> deviceOptional = DEVICE_MAP.values().stream()
                .filter(device -> devName.equals(device.getDevName()))
                .findFirst();
        if (deviceOptional.isPresent()){
            return deviceOptional.get();
        }else {
            ApiResult<Device> deviceApiResult = deviceClient.getByDevName(devName);
            Device device = deviceApiResult.getData();
            if (deviceApiResult.isSuccess() && device != null){
                DEVICE_MAP.put(device.getId(), device);
            }else {
                log.error("根据设备名称未获取到设备, 设备代号:{}", devName);
                return null;
            }
            return device;
        }
    }
}
